package net.realsee.controller.wcs.data;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import net.realsee.common.annotation.RepeatSubmit;
import net.realsee.common.annotation.Log;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.validate.AddGroup;
import net.realsee.framework.core.validate.EditGroup;
import net.realsee.framework.core.validate.QueryGroup;
import net.realsee.common.enums.BusinessType;
import net.realsee.common.utils.poi.ExcelUtil;
import net.realsee.domain.wcs.data.vo.WcsDefectTypeVo;
import net.realsee.domain.wcs.data.bo.WcsDefectTypeBo;
import net.realsee.service.wcs.data.IWcsDefectTypeService;
import net.realsee.framework.core.page.TableDataInfo;

/**
 * 不良类型定义
 *
 * @author realsee
 * @date 2023-08-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wcs/defectType")
public class WcsDefectTypeController extends BaseController {

    private final IWcsDefectTypeService iWcsDefectTypeService;

    /**
     * 查询不良类型定义列表
     */
    @SaCheckPermission("wcs:defectType:list")
    @GetMapping("/list")
    public TableDataInfo<WcsDefectTypeVo> list(WcsDefectTypeBo bo, PageQuery pageQuery) {
        return iWcsDefectTypeService.selectVoPage(bo, pageQuery);
    }

    /**
     * 导出不良类型定义列表
     */
    @SaCheckPermission("wcs:defectType:export")
    @Log(title = "不良类型定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WcsDefectTypeBo bo, HttpServletResponse response) {
        List<WcsDefectTypeVo> list = iWcsDefectTypeService.selectVoList(bo);
        ExcelUtil.exportExcel(list, "不良类型定义", WcsDefectTypeVo.class, response);
    }

    /**
     * 获取不良类型定义详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wcs:defectType:query")
    @GetMapping("/{id}")
    public R<WcsDefectTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWcsDefectTypeService.selectVoById(id));
    }

    /**
     * 新增不良类型定义
     */
    @SaCheckPermission("wcs:defectType:add")
    @Log(title = "不良类型定义", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WcsDefectTypeBo bo) {
        return toAjax(iWcsDefectTypeService.insertByBo(bo));
    }

    /**
     * 修改不良类型定义
     */
    @SaCheckPermission("wcs:defectType:edit")
    @Log(title = "不良类型定义", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WcsDefectTypeBo bo) {
        return toAjax(iWcsDefectTypeService.updateByBo(bo));
    }

    /**
     * 删除不良类型定义
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wcs:defectType:remove")
    @Log(title = "不良类型定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iWcsDefectTypeService.deleteWithValidByIds(Arrays.asList(ids)));
    }
}
